/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package combatlogparser.je;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class JEEnv {

	private static Environment env;
	private static EnvironmentConfig _envConfig;
	private static String _envHome;
	private static boolean _allowCreate;
	private static boolean _transactional;
	private static JEEnv instance;

	private JEEnv() {
	}

	public synchronized static JEEnv getInstance(String envHome, boolean allowCreate, boolean transactional) throws DatabaseException {
		if (instance == null) {
			instance = new JEEnv();
			_envHome = envHome;
			_allowCreate = allowCreate;
			_transactional = transactional;
			init();
		}
		return instance;
	}
	
	private static void init() {
		if (_allowCreate && !(new File(_envHome).exists())) {
			new File(_envHome).mkdirs();
		}
		_envConfig = new EnvironmentConfig();
		_envConfig.setAllowCreate(_allowCreate);
		_envConfig.setTransactional(_transactional);
	}

	public synchronized static JEEnv getInstance(String envHome) throws DatabaseException {
		return getInstance(envHome, true, false);
	}

	public Environment open() throws DatabaseException {
		if (env == null) {
			env = new Environment(new File(_envHome), _envConfig);
		}
		return env;
	}

	public void close() {
		if (env != null) {
			try {
				env.close();
			} catch (DatabaseException ex) {
				Logger.getLogger(JEEnv.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}
}
